package util;

import gnu.io.SerialPortEvent;

import java.util.BitSet;

public class Util {
	public static String printInts(int[] b){
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < b.length; i++){
			sb.append(b[i]);
			sb.append(", ");
		}
		return sb.toString();
	}
	
	public static String printBytes(byte[] b){
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < b.length; i++){
			sb.append(b[i]);
			sb.append(", ");
		}
		return sb.toString();
	}

	
	
	
	public static String printShorts(short[] b){
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < b.length; i++){
			sb.append(b[i]);
			sb.append(", ");
		}
		return sb.toString();
	}
	
	public static String printBits(byte[] b){
		StringBuffer sb = new StringBuffer();
		sb.append("Bits: ");
		for(int i = 0; i < b.length; i++){
			sb.append("byte ");
			sb.append(i);
			sb.append(": ");
			BitArray bits = new BitArray(b[i], 8);
			sb.append(bits.toString());
			sb.append(" ");
		}
		return sb.toString();
	}
	
	public static String printBits(byte b){
		return printBits(new byte[]{b});
	}
	
    // Returns a bitset containing the values in bytes.
    // The byte-ordering of bytes must be big-endian which means the most significant bit is in element 0.
    public static BitSet toBitSet(byte[] bytes) {
        BitSet bits = new BitSet(8);
        for (int i=0; i<bytes.length*8; i++) {
            if ((bytes[bytes.length-i/8-1]&(1<<(i%8))) > 0) {
                bits.set(i);
            }
        }
        return bits;
    }
    
    public static BitSet toBitSet(byte b){
    		return toBitSet(new byte[]{b});
    }
	/*
	public String printSerialEvent(SerialPortEvent e){
		StringBuffer sb = new StringBuffer();
		sb.append("SerialEvent Recieved. EventType: ");
		int eventType = e.getEventType();
		switch(eventType){
		case SerialPortEvent.BI: 					sb.append("BreakInterrupt "); break;
		case SerialPortEvent.CD: 					sb.append("CarrierDetect "); break;
		case SerialPortEvent.CTS: 				sb.append("ClearToSend "); break;
		case SerialPortEvent.DATA_AVAILABLE: 		sb.append("DataAvailable "); break;
		case SerialPortEvent.DSR: 				sb.append("DataSetReady "); break;
		case SerialPortEvent.FE: 					sb.append("FramingError "); break;
		case SerialPortEvent.OE: 					sb.append("OverrunError "); break;
		case SerialPortEvent.OUTPUT_BUFFER_EMPTY: 	sb.append("OutputBufferEmpty "); break;
		case SerialPortEvent.PE: 					sb.append("ParityError "); break;
		case SerialPortEvent.RI: 					sb.append("RingIndicator"); break;
		}
		if(e.getNewValue()) sb.append("true");
		else sb.append("false");
		return sb.toString();
	}
	*/
	public static int shiftLeftByte(int val, int s){
		val = val << s;
		return val & 0xFF;
	}
	
	public static int shiftLeftShort(int val, int s){
		val = val << s;
		return val & 0xFFFF;
		
	}
	
	public static int shiftRightByte(int val, int s){
		val = val >> s;
		return val & 0xFF;
	}
	
	public static int shiftRightShort(int val, int s){
		val = val >> s;
		return val & 0xFFFF;
	}
	
	public static int mask(int val){
		return val & 0x7F;
	}
	
	public static int byteToInt(byte val){
		int ret = val & 0xFF;
		if((val & 0x80) > 0) 
			ret += 0x80;
		return ret;
		
	}
	
	public static byte[] pack(int val){
		int msw = val >> 16;
		System.out.println("1. msw: " + msw);
		msw = msw & 0x8000;
		System.out.println("2. msw: " + msw);
		//int lsw = val >> 1;
		//System.out.println("3. lsw: " + lsw);
		int lsw = val | msw;
		System.out.println("4. lsw: " + lsw);
		lsw = lsw >> 1;
		System.out.println("5. lsw: " + lsw);
		int msb = lsw >> 8;
		System.out.println("6. msb: " + msb);
		int lsb = (lsw & 0xFF) >> 1;
		System.out.println("7. lsb: " + lsb);
		return new byte[]{ (byte)lsb, (byte)msb };
	}

	public static float setDecimals(float f, int d){
		int mlt = (int)Math.pow(10, d);
		f = (float)Math.round(f * mlt);
		return f / mlt;
	}
}
